/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module dp3201.c - Functions to Control DataPac 3201 communications |
   @end

$Log: /SoftPay/SPK2SIM/Source/Comm/dp3201.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>

#ifdef DP3201_DEFINED

#define VSDP3201_C

#include <txoinc.h>
#include <stdio.h>
#include <xmodem.h>
#include <ascii.h>
#include <acldev.h>
#include <aclconio.h>
#include <applidl.h>
#include <svctxo.h>
#include <string.h>
#include <actutil.h>

#ifdef DEBUG_VSDP3201
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <dp3201.h>
#include <comm.h>
#include <modem.h>
#include <modemmsg.h>
#include <confio.h>
#include <hdt.h>
#include <dpt.h>
#include <init.h>
#include <msgfuncs.h>
#include <msg.h>
#include <spdebug.h>
#include <mem.h>
#include <operate.h>

static long lnException;
int hModem; /* need a global modem variable for the exception trap */

/*-------------------------------------------------------------------------
    Function :    in3201TrapFail
    Description : This procedure is calling for the low level protocol
                  and will analize the exceptions (Traps) values and act
                  accordingly.
    Parameters :  trap_number
    Returns :     VS_ERR, VS_SUCCESS
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-20-95 Jose_F1    Created
      2   11/06/96 Jose_F1    Set Error Message to TXMT_FAIL.
 --------------------------------------------------------------------------*/

int in3201TrapFail(long exception) {
    /*  pdebug(("--in3201TrapFail--"));   */

    //  if ( (status = ioctlc(hModem,GetCtrl|GET_EXCEPTION_STATUS, &exception, 4)) < 0)
    //      {
    /*        printf("\ffail get_exception_status"); */
    //      return VS_ERR;
    //      }

    /** /pdebug(("3201TrapFail Exception = %d", exception));*/

    if (exception & PACKET_MADEIT)
        return VS_SUCCESS;

    if (exception & FIRST_POLL)
        return VS_SUCCESS;

    if (exception & XMIT_FAILURE)
        vdDisplayMessageFromFile(XMIT_FAILURE_3201_MSG21, STATUS_MSG);

    if (exception & BAD_LINE)
        vdDisplayMessageFromFile(BAD_LINE_3201_MSG21, STATUS_MSG);

    if (exception & STREAMING_ERR)
        vdDisplayMessageFromFile(STREAMING_ERR_3201_MSG21, STATUS_MSG);
    //NKJT IOCTL
    //  if((status = ioctl(hModem, SetCtrl|EMPTY_REJECT_Q,0)) < 0) /* empty rej q */
    //    {
    /*        printf("\ffail empty reject queue"); */
    //      return VS_ERR;
    //  }
    return VS_SUCCESS;
}

/*-------------------------------------------------------------------------
    Function : inInit3201Communication()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09/18/98 Monique_B1   Creation.
--------------------------------------------------------------------------*/

int inInit3201Communication(COMM_OBJECT* obCommunication) {
    DP3201_OBJECT* ob3201;

    /** /pdebug(("--inInit3201Communication--"));*/

    /*
     *      Allocate space for the object.
     */
    if ((obCommunication->Data = (void *) pchAlloc(sizeof(DP3201_OBJECT))) == NULL_CH)
        return (VS_ERROR);

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    ob3201->h3201 = VS_FALSE;

    return (VS_SUCCESS);
} /* End of inInit3201Communication() */

/*-------------------------------------------------------------------------
    Function :    inOpen3201Modem
    Description : Opens COM4 as Modem for 3201 use
    Parameters :  inCommType
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     hModem
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09-07-95 Carlos     Created
      2   10-04-95 Jose_F1    Add parameter to Open Init 3201.
                              Add Initialization call and pdebug.
      3   09/21/98 Monique_B1 Modified to use Base SoftPay structures
 --------------------------------------------------------------------------*/
int inOpen3201Modem(COMM_OBJECT* obCommunication,int inCPTIndex,VS_BOOL fUnusedParamter) {
    char szDevice[20];
    int inRetVal = 0;

    DP3201_OBJECT* ob3201;
    TRANSACTION_OBJECT* pobTran;

    /** /pdebug(("--inOpen3201Modem--"));*/


    ob3201 = (DP3201_OBJECT *) obCommunication->Data;
    if ((pobTran = pstGet_pobTran()) == NULL_CH)
        return VS_ERR;

    if (inLoadConfRec(DPT_FILE_NAME, SIZE_DPT_REC, inGetHostHandle(), (char *) &ob3201->srDPT) != VS_SUCCESS) {
        vdDisplayMessageFromFile(DPT_ERROR_MSG21, ERROR_MSG);
        return(VS_ERR);
    }

    /*
     *      Get Path for 3201 Port
     */
    if (inGetCommDevicePath(ob3201->srDPT.chIOPort, szDevice) == VS_ERR)
        return VS_ERR;

    if ((ob3201->h3201 = open(szDevice, 0)) > 0) {
        if ((inRetVal = inInit3201Modem(obCommunication)) != VS_SUCCESS) {
            /** /pdebug (("!set protocol(%d)",inRetVal));*/
            close(ob3201->h3201);
            ob3201->h3201 = VS_FALSE;
        } else
            obCommunication->fLinkUp = VS_TRUE;
    } else {
        ob3201->h3201 = VS_FALSE;
        /** /pdebug (("!3201 Open(%d/%d)",inHandle,errno));*/
    }

    return (inRetVal);
}


/*-------------------------------------------------------------------------
    Function :    inInit3201Modem
    Description : Initializes modem for 3201 use
    Parameters :  inCommType: Text or Transparent (7-bit or 8-bit)
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     hModem
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09-07-95 Carlos     Created
      2   10-04-95 Jose_F1    Added parameter to Init 3201 and pdebug.
      3   10-25-95 Jose_F1    Added parameter inCommType.
      4   01-17-96 Jose_F1    Modified 3201 Poll Code processing.
      5   09/21/98 Monique_B1 Modified to use Base SoftPay structures
      6   09/25/98 Monique_B1 Set protocol based on DPT flag instead of link type
      7   10/21/98 Monique_B1 Converted the Poll code from a hex string to an int
--------------------------------------------------------------------------*/
int inInit3201Modem(COMM_OBJECT* obCommunication) {
    struct Opn_Blk ob;
    int inPollCode;
    DP3201_OBJECT* ob3201;

    /*pdebug(("--inInit3201Modem--")); */

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    memset(&ob, 0, sizeof(ob));

    ob.rate = Rt_1200;              /* baud rate                        */
    ob.protocol = P_3201_fdx;           /* DATAPAC 4 wire full duplex       */

    /* Obtain the Poll code from CPT    */
    /* Poll address from str to hex     */

    inPollCode = hexstr2int(ob3201->srDPT.szPollCode); /* Converts str to hex             */
    /** /pdebug (("DPT PollCode = %d", inPollCode));*/


    if (inPollCode & 0x80)      /* Check if the leftmost bit is on 10000000b*/
        inPollCode = inPollCode ^ 0x80; /* Turn off leftmost bit XOR 10000000b*/

    //  ob.trailer.dp_parms.adr = (unsigned char)inPollCode; /* Poll Code                      */
    //  ob.trailer.dp_parms.n1  = (unsigned char)ob3201->srDPT.inNAKsSentRecvd;
    //  ob.trailer.dp_parms.n2  = (unsigned char)ob3201->srDPT.inSecsWithoutCarrier;
    //  ob.trailer.dp_parms.n3  = (unsigned char)ob3201->srDPT.inENQsSentRecvd;
    /* pdebug (("Conv. PollCode = %d", ob.trailer.dp_parms.adr));*/
    /*  pdebug (("inCommType = %d", obCommunication->inLinkType));  */

    switch(ob3201->srDPT.fProtocolMode) {
    case TEXT_MODE:
        /* DataPac 3201 PROTOCOL - Text Mode */

        /* pdebug (("Switch 3201 Text"));  */
        /* This open block config is for non-transparent mode (text)    */
        ob.format = Fmt_A7E1;        /* specify for text (A4_mode=TRUE) */
        //          ob.trailer.dp_parms.A4_mode = VS_TRUE; /* Text mode             */
        break;

    case BINARY_MODE:
        /* DataPac 3201 PROTOCOL TRANSPARENT MODE  */

        /* pdebug (("Switch 3201 Binary"));     */
        /* This open block config is for transparent mode (binary)      */
        ob.format = Fmt_A8N1;      /* transparent mode data format    */
        //          ob.trailer.dp_parms.A4_mode = VS_FALSE; /* transparent mode     */
        break;

    default:
        return(VS_ERR);
    }
    //   if( ioctl( ob3201->h3201, SetCtrl|0, &ob) < 0); /* Set Open Block */
    if (set_opn_blk(ob3201->h3201, &ob) < 0)
        return(VS_ERR);
    else
     /** /pdebug (("3201 Modem Init OK.")); */
        return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inCheck3201Poll
    Description : Checks the 3201 Poll Counts from Firmware
    Parameters :  inCommType
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     CurrPoll, PrevPoll_
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-05-95 Jose_F1    Created
      2   10-20-95 Jose_F1    Added loop using inGetCPTPollTimeOut
      3   09/21/98 Monique_B1 Modified to use Base SoftPay structures
      4   09/25/98 Monique_B1 Check clear key
--------------------------------------------------------------------------*/
int inCheck3201Poll(COMM_OBJECT* obCommunication) {
    static char prev_poll;
    static char curr_poll;
    unsigned long ulTicks;
    unsigned long ulTimeOut;
    DP3201_OBJECT* ob3201;


    /*pdebug(("--inCheck3201Poll--"));  */

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    /* Check Clear key press */
    if (act_kbd_pending_test(KEY_CANCEL)) {
        obCommunication->inEnd(obCommunication);
        vdSetTranError(TXN_CANCELLED);
        return (VS_ESCAPE);
    }

    obCommunication->fLinkUp = VS_FALSE;

    ulTimeOut = ob3201->srDPT.inPollTimeOut;

    /* Jose: 05/01/97 - Add a ioctlc checking for return value */

    /* Add Poll Counts for previous poll, later it will be checked */
    //
    //  if(ioctlc(ob3201->h3201, GetCtrl|GET_POLL_COUNT, &prev_poll, 1) < 0)
    //  {
    //       return(VS_ERR);
    //  }


    ulTicks = set_itimeout(NO_CLOCK_HANDLE(unsigned int)ulTimeOut, TM_SECONDS);
    do {
        if (CHECK_TIMEOUT(ulTicks) == 0)
            break;
        //
        //      ioctlc( ob3201->h3201, GetCtrl|GET_POLL_COUNT, &curr_poll, 1 );
        if (curr_poll != prev_poll)
            break;
    } while (1);

    /****
    if ( curr_poll == prev_poll )
    {
        0919-reset response code if no poll in case
          the response is in the middle of receiving
          and response is set to 899 which causing
          invoice number not increment*/

    /*   memset(srGDS.szRespCode, ' ', RESP_CODE_SIZE);
         return(VS_ERR);
    }
       ****/

    obCommunication->fLinkUp = VS_TRUE;
    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inCheck3201Link
    Description : Checks the 3201 Link
    Parameters :  inCommType
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     CurrPoll, PrevPoll_
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-05-95 Jose_F1    Created
      2   09/21/98 Monique_B1 Modified to use Base SoftPay structures
--------------------------------------------------------------------------*/
int inCheck3201Link(COMM_OBJECT* obCommunication) {
    DP3201_OBJECT* ob3201;

    /*pdebug(("--inCheck3201Link--"));  */

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    if (ob3201->h3201 == VS_FALSE)                      /* Check if was initialized    */ {
        ob3201->h3201 = inOpen3201Modem(obCommunication, 0);
        if (ob3201->h3201 == VS_FALSE)
            return(VS_ERR);
    }

    /* Check Exceptions first  ?          */

    /* Purge the reject queue                   */
    /*Jose: 11/01/97 - Modified the Call */
    //
    //    ioctl( ob3201->h3201, SetCtrl|EMPTY_REJECT_Q, 0);  /* empty rej q */

    /* Reset the Modem                          */
    //
    //  ioctl(ob3201->h3201, SetCtrl | 1, modem_status );
    reset_port_error(ob3201->h3201);

    /* Reset any Trap left                      */
    //  disarm_guard(inTrap3201Slot);

    /* Set global modem handle to local modem handle for in3201TrapFail */
    hModem = ob3201->h3201;

    /* Set Trap 48 for exceptions               */
    //  inTrap3201Slot = arm_guard(in3201TrapFail, mask_of(48), mask_of(48));

    /*  if (inTrap3201Slot < 0)
        {
            disarm_guard(inTrap3201Slot);
            return(VS_ERR);
        }
    */
    if (in3201SetTrap(&lnException) != VS_SUCCESS)
        return (VS_ERR);

    if (in3201CheckException(lnException) != VS_SUCCESS) {
        vdDisplayMessageFromFile(BAD_LINE_3201_MSG21, STATUS_MSG);
        return VS_ERR;
    }

    obCommunication->fLinkUp = VS_TRUE;
    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    in3201Finish
    Description : Reset the 3201 Comm
    Parameters :  n/a
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     hModem_
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-05-95 Jose_F1    Created
      2   09/21/98 Monique_B1 Modified to use Base SoftPay structures
--------------------------------------------------------------------------*/
int in3201Finish(COMM_OBJECT* obCommunication) {
    /*pdebug(("--in3201Finish--"));  */

    /* Reset any Trap left                      */
    //  disarm_guard(inTrap3201Slot);

    obCommunication->fLinkUp = VS_FALSE;
    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    in3201ReadRespPkt
    Description : The procedure Reads from the modem in a block mode.
                  The low level protocols handles the retries,
                  ACK and NAK, and LRC checks.
    Parameters :  n/a
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     hModem_
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-11-95 Jose_F1    Created
      2   10/09/96 Jose_F1   Added checking for Carrier drop.
      3   09/21/98 Monique_B1 Modified to use Base SoftPay structures
      4   09/25/98 Monique_B1 Added ETX & NULL_CH to end of receive packet
--------------------------------------------------------------------------*/
int in3201ReadRespPkt(COMM_OBJECT* obCommunication,char* pchReceiveBuff,int inReceiveSize,int inReceiveTimeout,long lnMessage) {
    unsigned long ulTicks;
    DP3201_OBJECT* ob3201;
    int inRetVal;

    pdebug(("--in3201ReadRespPkt--"));

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    if (obCommunication->fLinkUp == VS_FALSE)
        return VS_ERR;

    vdDisplayMessageFromFile(lnMessage, STATUS_MSG);

    memset(pchReceiveBuff, 0, GEN_BUF_SIZE);

    ulTicks = set_itimeout(NO_CLOCK_HANDLE inReceiveTimeout, TM_SECONDS);
    do {
        if (CHECK_TIMEOUT(ulTicks) == 0) {
            pdebug(("Read Timeout"));
            return(VS_TIMEOUT);
        }
        if ((inRetVal = read(ob3201->h3201, pchReceiveBuff, inReceiveSize)) < 0)
            return (VS_ERR);
        if (inRetVal > 0)
            break;
    } while (1);

    /*-----------------11/2/95 9:39:M-------------------
       Check if there was an exception in the 3201 link.
       If it was one, the inCheck3201TrapFail is being called
       by the low level protocol and an error is displayed.
    --------------------------------------------------*/

    if (in3201CheckException(lnException) != VS_SUCCESS)
        return VS_ERR;

    /* A message was read from the Modem
       Add ETX to the End of the Message              */

    /* ??? stLocal[inTotSize++] = ETX;
       stLocal[inTotSize] = '\0';
    memcpy(stBuf, stLocal, GEN_BUF_SIZE);
       */

    pchReceiveBuff[inReceiveSize] = ETX;
    pchReceiveBuff[inReceiveSize + 1] = NULL_CH;

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    in3201TxPacket
    Description : Transmits the packet, and wait for the result.
                  The low level protocol will set the status PACKET_MADE_IT
                  equal to 2 from GetCtrl = 11.
    Parameters :  n/a
    Returns :     VS_ERR, VS_SUCCESS
    Globals :     hModem_
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-11-95 Jose_F1    Created
      2   09/21/98 Monique_B1 Modified to use Base SoftPay structures
--------------------------------------------------------------------------*/

int in3201TxPacket(COMM_OBJECT* obCommunication,char* pchSendBuff,int inSendSize,VS_BOOL fConfirmSend,int inTimeout,long lnMessage) {
    DP3201_OBJECT* ob3201;

    pdebug(("--in3201TxPacket--"));

    ob3201 = (DP3201_OBJECT *) obCommunication->Data;

    vdDisplayMessageFromFile(lnMessage, STATUS_MSG);

    /* Purge the reject queue                   */
    //  ioctl( ob3201->h3201, GetCtrl | EMPTY_REJECT_Q, 0L );

    /* Reset the Modem                          */
    //  ioctl( ob3201->h3201, SetCtrl | 1, modem_status );
    reset_port_error(ob3201->h3201);

    /* Reset any Trap left                      */
    //  disarm_guard(inTrap3201Slot);

    /* Set global modem handle to local modem handle for in3201TrapFail */
    hModem = ob3201->h3201;

    /* Set Trap 48 for exceptions               */
    //  inTrap3201Slot = arm_guard(in3201TrapFail, mask_of(48), mask_of(48));

    /*  if (inTrap3201Slot < 0)
        {
            disarm_guard(inTrap3201Slot);
            return(VS_ERR);
        }
    */
    in3201SetTrap(&lnException);

    /* Send request packet to the host          */
    if (write(ob3201->h3201, pchSendBuff, inSendSize) != inSendSize)
        return (VS_ERR);

    if (obCommunication->fLinkUp == VS_FALSE)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    in3201CheckException
    Description : This procedure is calling to analize if there are exceptions
                 (Traps) values returned by the low level protocol.
    Parameters :
    Returns :     VS_ERR, VS_SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   11-02-95 Jose_F1    Created

--------------------------------------------------------------------------*/

int in3201CheckException(long exception) {
    /*  pdebug(("--in3201CheckException--"));  */

    //  if ( (status = ioctlc(hModem,GetCtrl|GET_EXCEPTION_STATUS, &exception, 4)) < 0)
    //  {
    /*        printf("\ffail get_exception_status");  */
    //      return VS_ERR;
    //  }
    /** /pdebug(("3201 Exception = %d", exception));*/
    if (exception & XMIT_FAILURE) {
        //
        //      if((status = ioctl(hModem, SetCtrl|EMPTY_REJECT_Q,0)) < 0) /* empty rej q */
        //          {
        /*            printf("\ffail empty reject queue"); */
        //          return VS_ERR;
        //      }
        return VS_ERR;
    }

    if (exception & BAD_LINE) {
        //
        //      if((status = ioctl(hModem, SetCtrl|EMPTY_REJECT_Q,0)) < 0) /* empty rej q */
        //          {
        /*            printf("\ffail empty reject queue"); */
        //          return VS_ERR;
        //      }
        return VS_ERR;
    }

    if (exception & STREAMING_ERR) {
        //
        //      if((status = ioctl(hModem, SetCtrl|EMPTY_REJECT_Q,0)) < 0) /* empty rej q */
        //          {
        /*            printf("\ffail empty reject queue"); */
        //          return VS_ERR;
        //      }
        return VS_ERR;
    }

    return VS_SUCCESS;
}


/*-------------------------------------------------------------------------
    Function :    in3201SetTrap
    Description : This procedure is seting the traps for 3201.
    Parameters :  n/a
    Returns :     VS_ERR, VS_SUCCESS
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   10-20-95 Jose_F1    Created

--------------------------------------------------------------------------*/
int in3201SetTrap(long* exception) {
    /*  pdebug(("--in3201SetTrap--"))  */

    *exception = XMIT_FAILURE | PACKET_MADEIT | BAD_LINE | FIRST_POLL | STREAMING_ERR;

    //  if ((status = ioctlc(hModem,SET_EXCEPTION_MASK|SetCtrl,&exception,4))<0)
    //  {
    /*          display("fail set exception mask"); */
    //        return VS_ERR;
    //  }

    //  if ((status=ioctlc(hModem,GET_EXCEPTION_MASK|GetCtrl, &exception, 4)) < 0)
    //  {
    /*        display("fail get exception mask"); */
    //      return VS_ERR;
    //  }

    if (*exception != (XMIT_FAILURE | PACKET_MADEIT | BAD_LINE | FIRST_POLL | STREAMING_ERR)) {
        /*        display("get_exception_mask_fail"); */
        return VS_ERR;
    }

    return VS_SUCCESS;
}

/*-------------------------------------------------------------------------
    Function : inDeInit3201Communication()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09/21/98 Monique_B1   Creation.
--------------------------------------------------------------------------*/
int inDeInit3201Communication(COMM_OBJECT* obCommunication) {
    /** /pdebug(("--inDeInit3201--"));*/
    vdFree(obCommunication->Data);
    obCommunication->inLinkType = NO_COMM;
    return (VS_SUCCESS);
} /* End of inDeInit3201Communication() */

/*-------------------------------------------------------------------------
    Function : vdInitialize3201Comm()
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   09/21/98 Monique_B1   Creation.
      2   09/25/98 Monique_B1 Change to handle both text and binary
--------------------------------------------------------------------------*/

void vdInitialize3201Comm(COMM_OBJECT* obCommunication) {
    /** /pdebug(("--vdInitialize3201Comm--"));*/
    obCommunication->inLinkType = COMM_3201; /* DataPac 3201 PROTOCOL */
    obCommunication->fLinkUp = VS_FALSE;
    obCommunication->inInitialize = inInit3201Communication;
    obCommunication->inBegin = inOpen3201Modem;
    obCommunication->inCheck = inCheck3201Poll;
    obCommunication->inSend = in3201TxPacket;
    obCommunication->inReceive = in3201ReadRespPkt;
    obCommunication->inEnd = in3201Finish;
    obCommunication->inFlush = NULL_CH;
    obCommunication->inDeinitialize = inDeInit3201Communication;
} /* End of vdInitialize3201Comm() */

/*-------------------------------------------------------------------------
    Function : power()
    Description : x to the power of y
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1             Canada   Creation.
--------------------------------------------------------------------------*/

unsigned long power(unsigned int x,unsigned int y) {
    unsigned  int i;
    unsigned long j;

    j = 1L;
    for (i = 0; i < y; ++i) {
        j *= (unsigned long) x;
    }
    return(j);
}


/*-------------------------------------------------------------------------
    Function : hexstr2int()
    Description : Converts a string of hex digits to an integer
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1             Canada   Creation.
--------------------------------------------------------------------------*/


int hexstr2int(char* str) {
    int len,
    powr = 0;
    int ret = 0;

    len = strlen(str) - 1;

    for (; len >= 0; --len, ++powr) {
        if (*(str + len) >= 0x41 && *(str + len) <= 0x46)
            ret += (*(str + len) - 0x37) * (int) power(16, powr);

        if (*(str + len) >= 0x30 && *(str + len) <= 0x39)
            ret += (*(str + len) - 0x30) * (int) power(16, powr);
    }
    return(ret);
}

#undef VSDP3201_C

#endif //DP3101_DEFINED

